home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’96 / PredatorPrey / ZEquals.c < prev    next >
Text File  |  1996-06-22  |  14KB  |  492 lines

  1. /* ZEquals.c -- Modal dialog */
  2. /* Created 940321 20:45 by AppMaker */
  3.  
  4. #include <Types.h>
  5. #include <Quickdraw.h>
  6. #include <Controls.h>
  7. #include <Dialogs.h>
  8. #include <Events.h>
  9. #include <Lists.h>
  10. #include <Menus.h>
  11. #include <TextEdit.h>
  12. #include "ResourceDefs.h"
  13. #include "Miscellany.h"
  14. #include "DialogAids.h"
  15. #include "WindowAids.h"
  16. #include "ZEquals.h"
  17. #include "ErrorWindow.h"
  18. #include <AppleEvents.h>
  19. //#include    <EPPC.h>
  20. #include    <PPCToolbox.h>
  21. #include    <Aliases.h>
  22. #include    <Files.h>
  23. #include    <Processes.h>
  24. #include    "calc_mousedown.h"
  25.  
  26.  
  27.  
  28. #define kFinderType 'FNDR'
  29. #define kSysCreator 'MACS'
  30. #define kAEOpenSelection 'sope'
  31. #define aeSelectionKeyword 'fsel'
  32.  
  33.  
  34. #define OKButton        1 
  35. #define CancelButton    2 
  36. #define ZLabel            3 
  37. #define Field4            4     /*    Y1 text    */
  38. #define ZEQUALSLabel    5 
  39. #define Button6            6     /*    ^        */
  40. #define Button7            7     /*    /        */
  41. #define Button8            8     /*    *        */
  42. #define Button9            9     /*    -        */
  43. #define Button10        10  /*    +        */
  44. #define Button11        11  /*    π        */
  45. #define eButton            12     /*    e^        */
  46. #define LogButton        13     /*    Log        */
  47. #define LnButton        14     /*    Ln        */
  48. #define SinButton        15     /*    Sin        */
  49. #define CosButton        16     /*    Cos        */
  50. #define TanButton        17     /*    Tan        */
  51. #define ArcTanButton    18     /*    ArcTan    */ 
  52. #define ArcSinButton    19     /*    ArcSin    */ 
  53. #define ArcCosButton    20     /*    ArcCos    */ 
  54. #define x10Button        21     /*    10^        */ 
  55. #define Button22        22     /*    √        */ 
  56. #define Button23        23     /*    (        */
  57. #define Button24        24     /*    )        */
  58. #define qButton            25     /*    theta    */
  59. #define x7Button        26     /*    7        */ 
  60. #define x8Button        27     /*    8        */ 
  61. #define x9Button        28     /*    9        */ 
  62. #define x4Button        29     /*    4        */ 
  63. #define x5Button        30     /*    5        */ 
  64. #define x6Button        31     /*    6        */ 
  65. #define x1Button        32     /*    1        */
  66. #define x2Button        33     /*    2        */ 
  67. #define x0Button        34     /*    0        */ 
  68. #define Button35        35     /*    decimal point    */ 
  69. #define x3Button        36     /*    3        */
  70. #define xxButton        37    /*    x        */
  71. #define ChkSynButton    38    /*    check syntax    */
  72. #define SelTgt            39    /*    select target    */
  73.         OSErr        FindAProcess        (
  74.                                             OSType typeToFind,
  75.                                             OSType creatorToFind,
  76.                                             ProcessSerialNumberPtr processSN,
  77.                                             ProcessInfoRecPtr infoRecToFill
  78.                                         );
  79.  
  80. static void DoChkSynButton ( short lastItem, ZEqualsRec *ZEquals, DialogPtr    theDialog);
  81.  
  82.  
  83. /*----------*/
  84.  
  85.  
  86. static void DoChkSynButton ( short lastItem, ZEqualsRec *ZEquals, DialogPtr    theDialog)
  87. {
  88.     short            i,j,k,m;
  89.     WindowPtr        errorWin;
  90.     ZEqualsRecPtr    info;
  91.     StringHandle    message;
  92.     Point            thePt;
  93.     Rect            Z1Rect;
  94.     
  95.     info = ZEquals;
  96.     j = 0;
  97.     k = 0;
  98.     m = 0;
  99.     SetRect(&Z1Rect,190,4,380,22);
  100.     
  101.     switch(lastItem)
  102.     {
  103.         case 4:
  104.                 MoveTo(4,0);
  105.                 for(i = 1;i <= info->Field4Text[0];i++)
  106.                 {
  107.                     if(info->Field4Text[i] == '(')    j++;
  108.                     if(info->Field4Text[i] == ')')    k++;
  109.                 }
  110.             break;
  111.     }
  112.     if( j == k )    ;    /*    do nothing    */
  113.     else
  114.     {
  115.         m++;
  116.         Move(0,m * 18);
  117.         GetPen(&thePt);
  118.         switch(lastItem)
  119.         {
  120.             case 4:
  121.                     EraseRect(&Z1Rect);
  122.                     BlockMove("\pIn the Z1 = field, there ",cur->Field3Handle,25);
  123.                     /*DrawString("\pIn the Z1 = field, there ");*/
  124.                 break;
  125.         }
  126.         if(j - k >= 2)
  127.         {
  128.             BlockMove("\pare ",cur->Field3Handle,4);
  129.             /*DrawString("\pare ");*/
  130.             message = GetString(3001);    /*    too many occurences of '\('    */
  131.             BlockMove(*message,cur->Field3Handle,GetHandleSize((Handle) message));
  132.  
  133.             /*DrawString(*message);*/
  134.             MoveTo(thePt.h,thePt.v);
  135.         }
  136.         else if(j - k == 1 || k - j == 1)
  137.         {
  138.             BlockMove("\pis ",cur->Field3Handle,3);
  139. /*            DrawString("\pis ");*/
  140.             message = GetString(3002);    /*    no '\)' found to match '\('    */
  141.             BlockMove(*message,cur->Field3Handle,GetHandleSize((Handle) message));
  142.             /*DrawString(*message);*/
  143.             MoveTo(thePt.h,thePt.v);
  144.         }
  145.         else if(k - j >=2)
  146.         {
  147.             BlockMove("\pare ",cur->Field3Handle,4);
  148. /*            DrawString("\pare ");*/
  149.             message = GetString(3000);    /*    too many occurences of '\)'    */
  150.             BlockMove(*message,cur->Field3Handle,GetHandleSize((Handle) message));
  151.             /*DrawString(*message);*/
  152.             MoveTo(thePt.h,thePt.v);
  153.         }
  154.     }
  155.  
  156. } /*DoChkSynButton*/
  157.  
  158. /*----------*/
  159. Boolean GetZEquals (ZEqualsRec    *ZEquals)
  160. {
  161.     DialogPtr        theDialog;
  162.     GrafPtr            savePort,save2Port;
  163.     Boolean            result;
  164.     Boolean            done;
  165.     short            itemNr;
  166.     register ZEqualsRecPtr    info;
  167.     short            i,lastItem;
  168.     TEHandle        dialogTE;
  169.     WindowPtr        saveCur,errorCur;
  170.     WindowPtr        errorWin = nil;
  171.     ModalFilterUPP    gMyStandardFilterUPP;
  172.     OSErr            iErr;
  173.     AEEventClass    theAEEventClass;
  174.     AEEventID        theAEEventID;
  175.     AppleEvent        theResult;
  176.     DescType    typeCode;
  177.     LocationNameRec        theLocation;
  178.     PortInfoRec         thePortInfo;
  179.     TargetID             theID;
  180.     AEDesc                 myAddressDesc;
  181.     AppleEvent             aeEvent, aeReply;
  182.     FSSpecPtr             theFileToOpen = nil;
  183.     FSSpec                dirSpec, procSpec;
  184.     AliasHandle            DirAlias, FileAlias;
  185.     ProcessSerialNumber    process;
  186.     ProcessInfoRec        infoRec;
  187.     StandardFileReply    myReply;
  188.     Str31                processName;
  189.     AEDesc                 aeDirDesc, listElem;
  190.     AEDesc                fileList;
  191.  
  192.     lastItem = 4;
  193.     GetPort (&savePort);
  194.     InitCursor ();
  195.     theDialog = GetNewDialog (ZEqualsID, nil, (WindowPtr) -1L);
  196.     SetPort (theDialog);
  197.     info = ZEquals;
  198.     SetDText (Field4, info->Field4Text);
  199.     
  200.     ShowWindow (theDialog);
  201.     OutlineButton (1);
  202.     gMyStandardFilterUPP = NewModalFilterProc(StandardFilter);    /*    new    */
  203.     done = false;
  204.     while (!done) {
  205.         
  206.         //ModalDialog (StandardFilter, &itemNr);
  207.         ModalDialog(gMyStandardFilterUPP,&itemNr);
  208.         dialogTE = ( (DialogPeek)theDialog)->textH;
  209.         switch (itemNr) {
  210.             case OKButton:
  211.                     result = true;
  212.                     done = true;
  213.                 break;
  214.             case CancelButton:
  215.                     result = false;
  216.                     done = true;
  217.                 break;
  218.             case Field4:
  219.                     GetDText (itemNr, info->Field4Text);
  220.                     lastItem = itemNr;
  221.                 break;
  222.             case Button6:
  223.                     TEKey(94,dialogTE);        /*    ^(    */
  224.                     TEKey(40,dialogTE);
  225.                                             /*    TEKey forces an update after every    */
  226.                                             /*    key, whereas SetIText will let you    */
  227.                                             /*    blast a bunch of chars to the field    */
  228.                                             /*    before the update                    */
  229.                 break;
  230.             case Button7:
  231.                     TEKey(47,dialogTE);        /*    /    */
  232.                 break;
  233.             case Button8:
  234.                     TEKey(42,dialogTE);        /*    *    */
  235.                 break;
  236.             case Button9:
  237.                     TEKey(45,dialogTE);        /*    -    */
  238.                 break;
  239.             case Button10:
  240.                     TEKey(43,dialogTE);        /*    +    */
  241.                 break;
  242.             case Button11:
  243.                     TEKey(185,dialogTE);        /*    π    */
  244.                 break;
  245.             case eButton:
  246.                     TEKey(101,dialogTE);        /*    e^(    */
  247.                     TEKey(94,dialogTE);
  248.                     TEKey(40,dialogTE);
  249.                 break;
  250.             case LogButton:
  251.                     TEKey(76,dialogTE);        /*    Log(    */
  252.                     TEKey(111,dialogTE);
  253.                     TEKey(103,dialogTE);
  254.                     TEKey(40,dialogTE);
  255.                 break;
  256.             case LnButton:
  257.                     TEKey(76,dialogTE);        /*    Ln(    */
  258.                     TEKey(110,dialogTE);
  259.                     TEKey(40,dialogTE);
  260.                 break;
  261.             case SinButton:
  262.                     TEKey(83,dialogTE);        /*    Sin(    */
  263.                     TEKey(105,dialogTE);
  264.                     TEKey(110,dialogTE);
  265.                     TEKey(40,dialogTE);
  266.                 break;
  267.             case CosButton:
  268.                     TEKey(67,dialogTE);        /*    Cos(    */
  269.                     TEKey(111,dialogTE);
  270.                     TEKey(115,dialogTE);
  271.                     TEKey(40,dialogTE);
  272.                 break;
  273.             case TanButton:
  274.                     TEKey(84,dialogTE);        /*    Tan(    */
  275.                     TEKey(97,dialogTE);
  276.                     TEKey(110,dialogTE);
  277.                     TEKey(40,dialogTE);
  278.                 break;
  279.             case ArcTanButton:
  280.                     TEKey(65,dialogTE);        /*    ArcTan(    */
  281.                     TEKey(114,dialogTE);
  282.                     TEKey(99,dialogTE);
  283.                     TEKey(84,dialogTE);
  284.                     TEKey(97,dialogTE);
  285.                     TEKey(110,dialogTE);
  286.                     TEKey(40,dialogTE);
  287.                 break;
  288.             case ArcSinButton:
  289.                     TEKey(65,dialogTE);        /*    ArcSin(    */
  290.                     TEKey(114,dialogTE);
  291.                     TEKey(99,dialogTE);
  292.                     TEKey(83,dialogTE);
  293.                     TEKey(105,dialogTE);
  294.                     TEKey(110,dialogTE);
  295.                     TEKey(40,dialogTE);
  296.                 break;
  297.             case ArcCosButton:
  298.                     TEKey(65,dialogTE);        /*    ArcCos(    */
  299.                     TEKey(114,dialogTE);
  300.                     TEKey(99,dialogTE);
  301.                     TEKey(67,dialogTE);
  302.                     TEKey(111,dialogTE);
  303.                     TEKey(115,dialogTE);
  304.                     TEKey(40,dialogTE);
  305.                 break;
  306.             case x10Button:
  307.                     TEKey(49,dialogTE);        /*    10^(    */
  308.                     TEKey(48,dialogTE);
  309.                     TEKey(94,dialogTE);
  310.                     TEKey(40,dialogTE);
  311.                 break;
  312.             case Button22:
  313.                     TEKey(195,dialogTE);     /*    √    */ 
  314.                     TEKey(40,dialogTE);
  315.                 break;
  316.             case Button23:
  317.                     TEKey(40,dialogTE);     /*    (    */
  318.                 break;
  319.             case Button24:
  320.                     TEKey(41,dialogTE);     /*    )    */
  321.                 break;
  322.             case qButton:
  323.                     TEKey(84,dialogTE);     /*    theta    */
  324.                     TEKey(104,dialogTE);
  325.                     TEKey(101,dialogTE);
  326.                     TEKey(116,dialogTE);
  327.                     TEKey(97,dialogTE);
  328.                 break;
  329.             case x7Button:
  330.                     TEKey(55,dialogTE);     /*    7    */
  331.                 break;
  332.             case x8Button:
  333.                     TEKey(56,dialogTE);     /*    8    */
  334.                 break;
  335.             case x9Button:
  336.                     TEKey(57,dialogTE);     /*    9    */
  337.                 break;
  338.             case x4Button:
  339.                     TEKey(52,dialogTE);     /*    4    */
  340.                 break;
  341.             case x5Button:
  342.                     TEKey(53,dialogTE);     /*    5    */
  343.                 break;
  344.             case x6Button:
  345.                     TEKey(54,dialogTE);     /*    6    */
  346.                 break;
  347.             case x1Button:
  348.                     TEKey(49,dialogTE);     /*    1    */
  349.                 break;
  350.             case x2Button:
  351.                     TEKey(50,dialogTE);     /*    2    */
  352.                 break;
  353.             case x0Button:
  354.                     TEKey(48,dialogTE);     /*    0    */
  355.                 break;
  356.             case Button35:
  357.                     TEKey(46,dialogTE);     /*    decimal point    */
  358.                 break;
  359.             case x3Button:
  360.                     TEKey(51,dialogTE);     /*    3    */
  361.                 break;
  362.             case xxButton:
  363.                     TEKey(120,dialogTE);     /*    x    */        
  364.                 break;
  365.             case ChkSynButton:
  366.                     GetDText (4, info->Field4Text);
  367.                     GetPort(&save2Port);        /*    GrafPtr        save2Port;    */
  368.                     /*saveCur = curWindow;*/        /*    WindowPtr    saveCur;    */
  369.                     if(errorWin == nil)
  370.                     {
  371.                         errorWin = OpenErrorWindow ("\p", 0, 0);    /*OpenErrorWindow (fName, vRefNum, fRefNum);*/
  372.                         /*SetPort(errorWin);*/
  373.                         /*errorCur = curWindow;*/
  374.                         /*SelectWindow(errorWin);*/
  375.                     }
  376.                     else
  377.                     {
  378.                         SetPort(errorWin);
  379.                     }
  380.                     
  381.                     DoChkSynButton ( 4/*lastItem*/ , ZEquals, theDialog);
  382.                     
  383.                     /*SetInfo (saveCur);*/
  384.                     SetPort(save2Port);
  385.                     SelectWindow((WindowPtr)save2Port);
  386.                 break;
  387.             case SelTgt:
  388.                 /*****************************/
  389.                     GetPort(&save2Port);
  390.                     StandardGetFile(nil, -1, nil, &myReply);
  391.                                     #ifdef DEBUGGING
  392.                                         DebugStr("\pRight after StandardGetFile");
  393.                                     #endif
  394.                     if (myReply.sfGood)
  395.                     {
  396.                                     #ifdef DEBUGGING
  397.                                         DebugStr("\pRight before theFileToOpen");
  398.                                     #endif
  399.                         theFileToOpen = &myReply.sfFile;
  400.                         infoRec.processInfoLength = sizeof(ProcessInfoRec);
  401.                         infoRec.processName = &processName[0];
  402.                         infoRec.processAppSpec = &procSpec;
  403.                                     #ifdef DEBUGGING
  404.                                         DebugStr("\pRight before FindAProcess");
  405.                                     #endif
  406.                         iErr = FindAProcess(kFinderType, kSysCreator, &process, &infoRec);
  407.                                     #ifdef DEBUGGING
  408.                                         DebugStr("\pRight after FindAProcess");
  409.                                     #endif
  410.                         if (iErr == noErr)
  411.                             iErr = AECreateDesc(typeProcessSerialNumber, (Ptr)&process, sizeof(process), &myAddressDesc);
  412.                                     #ifdef DEBUGGING
  413.                                         DebugStr("\pRight after AECreateDesc");
  414.                                     #endif
  415.                         if (iErr == noErr)
  416.                         {
  417.                             iErr = AECreateAppleEvent    (
  418.                                                             kFinderType, kAEOpenSelection,
  419.                                                             &myAddressDesc, kAutoGenerateReturnID,
  420.                                                             kAnyTransactionID, &aeEvent
  421.                                                         );
  422.                             AEDisposeDesc(&myAddressDesc);
  423.                                     #ifdef DEBUGGING
  424.                                         DebugStr("\pRight after AECreateAppleEvent");
  425.                                     #endif
  426.                             if (iErr == noErr)
  427.                             {
  428.                                 iErr = FSMakeFSSpec(theFileToOpen->vRefNum, theFileToOpen->parID, nil, &dirSpec);
  429.                                 NewAlias(nil, &dirSpec, &DirAlias);
  430.                                 NewAlias(nil, theFileToOpen, &FileAlias);
  431.                                     #ifdef DEBUGGING
  432.                                         DebugStr("\pRight after NewAlias");
  433.                                     #endif
  434.                             }
  435.                             if (iErr == noErr)
  436.                             {
  437.                                 iErr = AECreateList(nil, 0, false, &fileList);
  438.                                 
  439.                                 /*  create the folder  descriptor */
  440.                                 HLock((Handle)DirAlias);
  441.                                 AECreateDesc(typeAlias, (Ptr)*DirAlias, GetHandleSize((Handle)DirAlias), &aeDirDesc);
  442.                                 HUnlock((Handle)DirAlias);
  443.                                 if ((iErr = AEPutParamDesc(&aeEvent, keyDirectObject, &aeDirDesc)) == noErr)
  444.                                 {
  445.                                     /* done with the desc, kill it */
  446.                                     AEDisposeDesc(&aeDirDesc);
  447.                                     /*  create the file descriptor and add to aliasList */
  448.                                     HLock((Handle)FileAlias);
  449.                                     AECreateDesc(typeAlias, (Ptr)*FileAlias, GetHandleSize((Handle)FileAlias), &listElem);
  450.                                     HLock((Handle)FileAlias);
  451.                                     iErr = AEPutDesc(&fileList, 0, &listElem);
  452.                                 }
  453.                                 if (iErr == noErr)
  454.                                 {
  455.                                     AEDisposeDesc(&listElem);
  456.                                     
  457.                                     /* Add the file alias list to the event */
  458.                                     iErr = AEPutParamDesc(&aeEvent, aeSelectionKeyword, &fileList);
  459.                                     AEDisposeDesc(&fileList);
  460.                                     
  461.                                     if (iErr == noErr)
  462.                                         iErr = AESend(&aeEvent, &aeReply, kAENoReply + kAEAlwaysInteract + kAECanSwitchLayer,
  463.                                                        kAENormalPriority, kAEDefaultTimeout, nil, nil);
  464.                                 }
  465.                             }
  466.                             AEDisposeDesc(&aeEvent);
  467.                         }
  468.                     }
  469.                     SetPort(save2Port);
  470.                     SelectWindow((WindowPtr)save2Port);
  471.                 /*****************************/
  472.                 break;
  473.         } /* switch */
  474.     } /* while */
  475.     //DisposHandle ((Handle) cur->filename);
  476.     //DiscardInfo (curWindow);
  477.  
  478.     
  479.     //theAEEventID =  ;
  480.     //theAEEventClass = 'aevt';
  481.     //iErr = AECreateDesc(typeCode,dataPtr,dataSize,&result);
  482.     //iErr = AECreateAppleEvent(theAEEventClass,theAEEventID,&target,returnID,transactionID,&theResult);
  483.     //AESend(result);
  484.     AESend(&aeEvent, &aeReply, kAENoReply + kAEAlwaysInteract + kAECanSwitchLayer,
  485.                                     kAENormalPriority, kAEDefaultTimeout, nil, nil);
  486.     DisposDialog (theDialog);
  487.     SetPort (savePort);
  488.     return (result);
  489. } /*GetYEquals*/
  490.  
  491. /* YEquals */
  492.